﻿// =================================================================== 
// 项目说明
//====================================================================
// lsc @Copy Right 2019
// 文件： GovMenuRepository.cs
// 项目名称：斑马车务
// 创建时间：2019-01-09
// 负责人：lsc
// ===================================================================
using System;
using System.Text;
using System.Collections.Generic;
using CGT.Infrastructure.Entity;
using CGT.Infrastructure.Dto;
using Dapper.Extensions;
using Dapper;
using System.Linq;

namespace CGT.Infrastructure.Repository
{
	/// <summary>
	///菜单 仓储层
	/// </summary>	
	public class GovMenuRepository:BaseRepository<GovMenu>
	{		
    
        #region 构造

        public GovMenuRepository() : base(){}

        public GovMenuRepository(string connectionString) : base(connectionString){}

        public GovMenuRepository(DatabaseContext dbContext) : base(dbContext){}

        #endregion

        /// <summary>
        /// 获取有效数据
        /// </summary>
        /// <returns></returns>
        public List<GovMenu> GetValidList()
        {
            string sql = "select * from GovMenu where isactive=1 order by OrderNum asc";
            return Query(sql);
        }

        /// <summary>
        /// 获取父级菜单
        /// </summary>
        /// <returns></returns>
        public List<GovMenu> GetParentList()
        {
            string sql = "select * from GovMenu where IsActive=1 and ParentID=0 order by OrderNum asc";
            return Query(sql);
        }

        /// <summary>
        /// 编码是否重复
        /// </summary>
        /// <returns></returns>
        public bool CheckMenuCode(int code)
        {
            string sql = "select * from GovMenu where IsActive=1 and MenuCode=@MenuCode order by OrderNum asc";
            return ExecuteScalar<int>(sql, new { MenuCode = code }) > 0 ;
        }

        /// <summary>
        /// 获取菜单(并显示根据角色ID获取已关联的菜单)
        /// </summary>
        /// <returns></returns>
        public List<GovRoleMenuRelaDto> GetRoleMenuRelaList(int roleId)
        {
            string sql = "SELECT a.*,ISNULL(b.RelaID,0) as RelaID FROM GovMenu a left join GovRoleMenuRela b on a.MenuID = b.MenuID and b.IsActive = 1 and b.RoleID=@RoleID where a.IsActive = 1";
            return dbContext.Connection.Query<GovRoleMenuRelaDto>(sql, new { RoleID =roleId}, dbContext.Transcation).ToList(); 
        }

        /// <summary>
        /// 根据角色ID获取菜单
        /// </summary>
        /// <param name="roleId"></param>
        /// <returns></returns>
        public List<GovMenu> GetUserMenuList(int roleId)
        {
            string sql = "SELECT a.* FROM [GovMenu] a INNER JOIN [GovRoleMenuRela] b ON a.MenuID = b.MenuID  WHERE a.IsActive=1 and b.isActive=1 and b.RoleID= @RoleID ORDER BY [OrderNum] ";
            return Query(sql, new { RoleID = roleId });
        }

        /// <summary>
        /// 根据角色ID、菜单编码获取菜单信息
        /// </summary>
        /// <param name="roleId"></param>
        /// <param name="menuCode"></param>
        /// <returns></returns>
        public GovMenu GetUserMenuByRoleIdMenuCode(int roleId, int menuCode)
        {
            string sql = "SELECT a.* from govMenu a with(nolock) inner join GovRoleMenuRela b with(nolock) on a.menuid = b.menuid where a.IsActive=1 and b.IsActive=1 and a.MenuCode = @menuCode and b.RoleID = @roleId";
            return FirstOrDefault(sql, new { RoleID = roleId, MenuCode = menuCode });
        }

        /// <summary>
        /// 根据菜单编码获取菜单信息
        /// </summary>
        /// <returns></returns>
        public GovMenu GetGovMenuByCode(int code)
        {
            string sql = "select * from GovMenu where IsActive=1 and MenuCode=@MenuCode order by OrderNum asc";
            return Single(sql, new { MenuCode = code });
        }

        /// <summary>
        /// 删除父级下的所有子菜单
        /// </summary>
        /// <param name="menuId"></param>
        /// <returns></returns>
        public bool DelGovMenuByParentId(int parentId)
        {
            string sql = "update GovMenu set IsActive=0 where ParentID=@ParentID and isactive=1";
            return Execute(sql, new { ParentID = parentId }) > 0;
        }
    }
}